package com.hnevc.liangxiangju;

import java.sql.*;

/**
 *
 * 使用JDBC访问数据库
 * root root 3307 (默认3306)用户名 密码 端口号
 * localhost 访问地址
 * user 数据库名
 * student 数据库表名
 */
public class Example01 {
    public static void main(String[] args) {
        //1、注册数据库驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //2、通过DriverManager获取数据库连接
            String url ="jdbc:mysql://localhost:3307/student";
            String username ="root";
            String password ="root";
            Connection connection = DriverManager.getConnection(url,username,password);
            //获取statement对象
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from student");
            while (resultSet.next()){
            System.out.println(resultSet.getString("name")+resultSet.getString("department"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
